Telegram Group & Telegram Channel
🎯 Как настроить Spring Security с кастомной формой логина

Безопасность — базовая вещь. Spring Security по умолчанию кидает вас в дефолтную форму логина. Но в реальности — нужна своя, кастомная.

1️⃣ Добавьте зависимость
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>


2️⃣ Создайте SecurityConfig
@Configuration
@EnableWebSecurity
public class SecurityConfig {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/login", "/css/**").permitAll()
.anyRequest().authenticated()
)
.formLogin(form -> form
.loginPage("/login")
.defaultSuccessUrl("/dashboard", true)
.permitAll()
)
.logout(logout -> logout
.logoutSuccessUrl("/login?logout")
);

return http.build();
}

@Bean
public UserDetailsService userDetailsService() {
var user = User.withUsername("admin")
.password("{noop}admin123") // Для простоты, без шифрования
.roles("ADMIN")
.build();
return new InMemoryUserDetailsManager(user);
}
}


3️⃣ Создайте кастомную HTML-форму

src/main/resources/templates/login.html (если используете Thymeleaf):
<!DOCTYPE html>
<html>
<head><title>Login</title></head>
<body>
<h2>Login</h2>
<form method="post" action="/login">
<label>Username: <input type="text" name="username" /></label><br/>
<label>Password: <input type="password" name="password" /></label><br/>
<button type="submit">Login</button>
</form>
</body>
</html>


4️⃣ Реализуете контроллер
@RestController
public class DashboardController {

@GetMapping("/dashboard")
public String dashboard() {
return "Welcome to the dashboard!";
}
}


🐸 Библиотека джависта #буст
Please open Telegram to view this post
VIEW IN TELEGRAM



tg-me.com/javaproglib/6659
Create:
Last Update:

🎯 Как настроить Spring Security с кастомной формой логина

Безопасность — базовая вещь. Spring Security по умолчанию кидает вас в дефолтную форму логина. Но в реальности — нужна своя, кастомная.

1️⃣ Добавьте зависимость

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>


2️⃣ Создайте SecurityConfig
@Configuration
@EnableWebSecurity
public class SecurityConfig {

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests(auth -> auth
.requestMatchers("/login", "/css/**").permitAll()
.anyRequest().authenticated()
)
.formLogin(form -> form
.loginPage("/login")
.defaultSuccessUrl("/dashboard", true)
.permitAll()
)
.logout(logout -> logout
.logoutSuccessUrl("/login?logout")
);

return http.build();
}

@Bean
public UserDetailsService userDetailsService() {
var user = User.withUsername("admin")
.password("{noop}admin123") // Для простоты, без шифрования
.roles("ADMIN")
.build();
return new InMemoryUserDetailsManager(user);
}
}


3️⃣ Создайте кастомную HTML-форму

src/main/resources/templates/login.html (если используете Thymeleaf):
<!DOCTYPE html>
<html>
<head><title>Login</title></head>
<body>
<h2>Login</h2>
<form method="post" action="/login">
<label>Username: <input type="text" name="username" /></label><br/>
<label>Password: <input type="password" name="password" /></label><br/>
<button type="submit">Login</button>
</form>
</body>
</html>


4️⃣ Реализуете контроллер
@RestController
public class DashboardController {

@GetMapping("/dashboard")
public String dashboard() {
return "Welcome to the dashboard!";
}
}


🐸 Библиотека джависта #буст

BY Библиотека джависта | Java, Spring, Maven, Hibernate




Share with your friend now:
tg-me.com/javaproglib/6659

View MORE
Open in Telegram


Библиотека джависта | Java Spring Maven Hibernate Telegram | DID YOU KNOW?

Date: |

A Telegram spokesman declined to comment on the bond issue or the amount of the debt the company has due. The spokesman said Telegram’s equipment and bandwidth costs are growing because it has consistently posted more than 40% year-to-year growth in users.

For some time, Mr. Durov and a few dozen staffers had no fixed headquarters, but rather traveled the world, setting up shop in one city after another, he told the Journal in 2016. The company now has its operational base in Dubai, though it says it doesn’t keep servers there.Mr. Durov maintains a yearslong friendship from his VK days with actor and tech investor Jared Leto, with whom he shares an ascetic lifestyle that eschews meat and alcohol.

Библиотека джависта | Java Spring Maven Hibernate from sa


Telegram Библиотека джависта | Java, Spring, Maven, Hibernate
FROM USA